package cn.bonoon.kernel.menus.tips;

import javax.persistence.Query;

import org.batatasframework.web.RequestParameterReader;

import cn.bonoon.kernel.expression.ExpressionValue;
import cn.bonoon.kernel.expression.QueryExpressionWrapper;
import cn.bonoon.kernel.support.IOperator;

public class SqlMenuTipHandler extends AbstractMenuTipHandler {

	private final QueryExpressionWrapper queryExpression;
	
	public SqlMenuTipHandler(String sql) throws Exception{
		this.queryExpression = QueryExpressionWrapper.valueOf(sql);
	}
	
	@Override
	protected void internalHandler(IOperator operator, RequestParameterReader reader, MenuTipResult result) throws Exception {
		Query query = platformContext.sqlQuery(queryExpression.toString());
		int paramenterIndex = 1;
		for(ExpressionValue eValue : queryExpression){
			query.setParameter(paramenterIndex++, eValue.value(operator, reader, null));
		}
		result.result(query.getResultList());
	}
}
